#include "stdafx.h"
using namespace std;

Mod_Interception::Mod_Interception(Mod_in *iinp):Mod_out_handling(iinp)
{
	;
}
Mod_Interception::~Mod_Interception  ()
{;}
int Mod_Interception::compute(time_intervall &ti){}
Mod_Interception_simple::Mod_Interception_simple (map <string,int> &iIO_options,
                         Mod_in *iinp,
                         Inp_weather *iweather,
                         Mod_ETP *imod_etp,
                         Mod_Crop *imodcrop
                ):Mod_Interception(iinp)
{
    //1. switches setzen
    IO_options=iIO_options;
    //2. Input_objekt setzen
    
    
    //3. Parameter definieren
    par_base=0;
    par_LAI=0.2;
    fraction_of_throughfall=0.4;
    
    //4. Modelle verknüpfen
    inp_weather= iweather;
    mod_etp=imod_etp;
    mod_crop=imodcrop;
    
    
    //5. Output_kategorien für diese Variante setzen!!
    out->fluxes.set_key("P_Throughfall");
    out->fluxes.set_key("ETR_Interzeption");
    timepointmap <double> null_timepointmap(inp->tempgrid);
   
    out->states["Interzeption_Storage"]=null_timepointmap;
    
}
Mod_Interception_simple::~Mod_Interception_simple(){;} 
int Mod_Interception_simple::compute(time_intervall &ti)
{
	double budget;
        timestamp start=ti.get_start();
        timestamp end=ti.get_end();
	double LAI=this->mod_crop->get_states ("LAI",start);
	double Interception_Storage_last=this->get_states("Interception_Storage",start);
	double P=this->inp_weather->get_rate("P",ti)*ti.get_dur();
	double Max_Interception_Storage=par_base+par_LAI*LAI;
	
	double ETP_Interception=this->get_fluxes("ETP_Interception",ti);
	double Interception_Storage_recent=Interception_Storage_last-ETP_Interception*ti.get_dur();
	
	double Interception_Storage_change=min(P*(1-fraction_of_throughfall),
		(Max_Interception_Storage-Interception_Storage_recent));
		
	double P_throughfall=max(0.0,P-Interception_Storage_change)/ti.get_dur();	  //[mm/d]
	
	this->set_push_back_fluxes("P_Throughfall",ti, P_throughfall);
	this->set_states("Interception_Storage",end,
		Interception_Storage_recent+Interception_Storage_change);
	
	budget=this->get_states("Interception_Storage", end)
			-Interception_Storage_change
			-ETP_Interception   ;
	if ((budget<-0.01)||(budget>0.01)) err::err2(  "Mod_Surface_water_simple::compute: Budget_is_not_0.!");
	if(this->get_states("Interception_Storage", end)<0.001) 
		err::err3(  "Mod_Interzeption_simple::compute: Interception_Storage<0");
	return 0;
}